package com.gourd.web.controller;

import com.gourd.common.data.BaseResponse;
import com.gourd.web.es.entity.RbacUserEs;
import com.gourd.web.es.repositoty.RbacUserRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;


/**
 * @author gourd
 */
@RestController
@RequestMapping("/user-es")
@Api(tags = "userEs", description = "Es用户控制器")
public class UserEsController {

	@Autowired
	private RbacUserRepository rbacUserRepository;

	/**
	 * 根据id查询
	 * @param id
	 * @return
	 */
	@GetMapping("/{id}")
	@ApiOperation(value = "根据id查询")
	public RbacUserEs getRbacUserEsById(@PathVariable Long id) {
		Optional<RbacUserEs> userEsOptional = rbacUserRepository.findById(id);
		return userEsOptional.get();
	}

	/**
	 * 全文精确检索
	 * @param keyword
	 * @return
	 */
	@GetMapping("filter/{keyword}")
	@ApiOperation(value = "全文精确检索")
	public List<RbacUserEs> findRbacUserEs(@PathVariable String keyword) {
		QueryStringQueryBuilder builder = new QueryStringQueryBuilder(keyword);
		Iterable<RbacUserEs> searchResult = rbacUserRepository.search(builder);
		Iterator<RbacUserEs> iterator = searchResult.iterator();
		List<RbacUserEs> list = new ArrayList<RbacUserEs>();
		while (iterator.hasNext()) {
			list.add(iterator.next());
		}
		return list;
	}

	/**
	 * 分页精确检索
	 * @param page
	 * @param size
	 * @param keyword
	 * @return 
	 * @return
	 */
	@GetMapping("/find/{page}/{size}/{keyword}")
	@ApiOperation(value = "分页精确检索")
	public Page<RbacUserEs> findRbacUserEsByPage(@PathVariable Integer page, @PathVariable Integer size, @PathVariable String keyword) {
		// 分页参数
		Pageable pageable = PageRequest.of(page,size);
		// 构建查询条件
		NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
		// 添加基本分词查询
		queryBuilder.withQuery(QueryBuilders.multiMatchQuery(keyword,"name","nickName"));
		// 设置分页
		queryBuilder.withPageable(pageable);
		// 构建查询
		Page<RbacUserEs> searchPageResults = rbacUserRepository.search(queryBuilder.build());
		return searchPageResults;

	}

	/**
	 * 分页模糊检索
	 * @param page
	 * @param size
	 * @param keyword
	 * @return
	 * @return
	 */
	@GetMapping("/search/{page}/{size}/{keyword}")
	@ApiOperation(value = "分页模糊检索")
	public Page<RbacUserEs> searchRbacUserEsByPage(@PathVariable Integer page, @PathVariable Integer size, @PathVariable String keyword) {
		// 分页参数
		Pageable pageable = PageRequest.of(page,size);
		BoolQueryBuilder builder = QueryBuilders.boolQuery();
		// builder下有must、should以及mustNot 相当于sql中的and、or以及not
		// 设置分词模糊搜索
		builder.must(QueryBuilders.fuzzyQuery("name", keyword))
				.should(QueryBuilders.fuzzyQuery("nickName", keyword));
		// 设置排序规则
		FieldSortBuilder sort = SortBuilders.fieldSort("id").order(SortOrder.DESC);
		// 构建查询
		NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
		// 将搜索条件设置到构建中
		nativeSearchQueryBuilder.withQuery(builder);
		// 将分页设置到构建中
		nativeSearchQueryBuilder.withPageable(pageable);
		// 将排序设置到构建中
		nativeSearchQueryBuilder.withSort(sort);
		// 生产NativeSearchQuery
		NativeSearchQuery query = nativeSearchQueryBuilder.build();
		Page<RbacUserEs> searchPageResults = rbacUserRepository.search(query);
		return searchPageResults;
	}

	/**
	 * 新增数据
	 * @param RbacUserEs
	 * @return
	 */
	@PostMapping("/add")
	@ApiOperation(value = "新增数据")
	public BaseResponse insertRbacUserEs(@RequestBody RbacUserEs RbacUserEs) {
		rbacUserRepository.save(RbacUserEs);
		return BaseResponse.ok("新增成功");
	}

	/**
	 * 根据id删除
	 * @param id
	 * @return
	 */
	@DeleteMapping("/{id}")
	@ApiOperation(value = "根据id删除数据")
	public BaseResponse deleteRbacUserEs(@PathVariable Long id) {
		RbacUserEs rbacUserEs = rbacUserRepository.findById(id).get();
		if(rbacUserEs == null){
			return BaseResponse.failure("未查询到信息");
		}
		rbacUserRepository.deleteById(id);
		return BaseResponse.ok("删除成功");
	}

	/**
	 * 修改数据
	 * @param RbacUserEs
	 * @return
	 */
	@PutMapping("/update")
	@ApiOperation(value = "修改数据")
	public BaseResponse updateRbacUserEs(@RequestBody RbacUserEs RbacUserEs) {
		rbacUserRepository.save(RbacUserEs);
		return BaseResponse.ok("修改成功");
	}

}